<template>
  <div style="width: 100%;margin: 10px 0 0 0;">
    <el-row v-show="showSearch">
      <el-col class="form_col">
        <el-form :model="queryParams" ref="queryForm" key="meterLogQueryForm" :size="formItemSize" v-show="showSearch"
                 :label-width="labelWidth">
          <div style="display: flex; justify-content: start; flex-wrap: wrap;">
            <el-form-item label="命令内容" prop="command">
              <el-select :size="formItemSize"
                         style="width: 100%;"
                         v-model="queryParams.command"
                         placeholder="请选择命令内容"
                         @change="handleQuery">
                <el-option label="关阀" :value="0"></el-option>
                <el-option label="开阀" :value="1"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="发送时间" prop="sendTime">
              <el-date-picker :size="formItemSize"
                              v-model="timevalue"
                              type="daterange"
                              value-format="yyyy-MM-dd"
                              style="width: 100%;"
                              placeholder="请选择发送时间"
                              clearable
                              @change="handleQuery"/>
            </el-form-item>
          </div>
        </el-form>
      </el-col>

      <el-col class="form_query">
        <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
        <el-button @click="resetQuery" size="mini">重置</el-button>
        <RightDropdown :dayColumns="valveColumns" :queryVisible="false" style="margin-left: 10px;"
                       :showSearch.sync="showSearch" @queryTable="getList"
                       :hasPermiExport="['basedata:xbCommandLogs:export']"
                       @export="handleExport"></RightDropdown>
      </el-col>
    </el-row>

    <el-row>
      <el-table v-loading="loading" :data="valveLogsList" border size="mini" :height="getAvailableHeight">
        <el-table-column label="#" type="index" align="center" width="40"/>
<!--        <el-table-column label="设备编号" align="center" prop="terminalId" v-if="valveColumns[0].visible"/>-->
        <el-table-column label="命令内容" align="center" prop="command" v-if="valveColumns[0].visible">
          <template slot-scope="scope">
            {{ ['关阀', '开阀'][scope.row.command] }}
          </template>
        </el-table-column>
        <el-table-column label="发送反馈" align="center" prop="feedback" v-if="valveColumns[1].visible"/>
        <el-table-column label="发送时间" align="center" prop="sendTime" width="180"
                         v-if="valveColumns[2].visible">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" v-if="valveColumns[3].visible"/>
      </el-table>
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-row>

  </div>
</template>

<script>
import moment from "moment/moment";
import {listXbCommandLogs} from "@/api/logs/xbCommandLogs";

export default {
  name: "MeteredLog",
  data() {
    return {
      screenHeight: null,
      loading: false,
      // 显示搜索条件
      showSearch: true,
      collapseAble: false,
      formItemSize: 'mini',
      maxShow: 4,
      labelWidth: '80px',
      // 总条数
      total: 0,
      // 表格数据
      valveLogsList: [],
      // 列信息
      valveColumns: [
        // {key: 0, label: `设备编号`, visible: true, dataIndex: 'terminalId'},
        {key: 1, label: `命令内容`, visible: true, dataIndex: 'command'},
        {key: 2, label: `发送反馈`, visible: true, dataIndex: 'feedback'},
        {key: 3, label: `发送时间`, visible: true, dataIndex: 'sendTime'},
        {key: 4, label: `备注`, visible: true, dataIndex: 'remark'},
      ],
      // 发送时间
      timevalue: [moment().subtract(6, 'day').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        command: null,
        terminalId: null,
        locationNb: null,
        sendTime: null,
        params: {
          beginDate: null,
          endDate: null
        },
      },
    }
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 266
    }
  },
  methods: {
    updateLocationNb(locationNb){
      this.queryParams.locationNb = locationNb
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    // 重置表单
    resetQuery() {
      this.timevalue = [moment().subtract(6, 'day').format('YYYY-MM-DD'), moment().format('YYYY-MM-DD')]
      this.queryParams = {
        pageNum: 1,
        pageSize: 20,
        command: null,
        terminalId: null,
        sendTime: null,
        params: {
          beginDate: null,
          endDate: null
        },
      }
      this.handleQuery()
    },
    // 获取表格数据
    getList() {
      this.loading = true;
      this.queryParams.params.beginDate = this.timevalue[0]
      this.queryParams.params.endDate = this.timevalue[1]
      listXbCommandLogs(this.queryParams).then(response => {
        if (response.code === 200) {
          this.valveLogsList = response.rows
          this.loading = false;
          this.total = response.total
        } else {
          this.$modal.msgError("数据查看失败!")
          this.loading = false;
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/xbCommandLogs/export', {
        ...this.queryParams,
      }, `水表阀控记录数据.xlsx`)
    }
  }

}
</script>

<style scoped lang="scss">
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 27%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
